package com.datang.petstore.service.impl;


import com.alibaba.fastjson.JSON;
import com.datang.petstore.entity.Microblog;
import com.datang.petstore.entity.MicroblogComment;
import com.datang.petstore.exception.AcceptResultCodeException;
import com.datang.petstore.generatemapper.MicroblogCommentMapper;
import com.datang.petstore.mapper.MicroblogCommentCustomMapper;
import com.datang.petstore.model.GetMicroblogCommentListRequest;
import com.datang.petstore.model.GetMicroblogCommentListResponse;
import com.datang.petstore.model.GetMicroblogTwoCommentListResponse;
import com.datang.petstore.resultpage.ResultCode;
import com.datang.petstore.service.MicroblogCommentService;
import com.datang.petstore.service.MicroblogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
/*
 * @auther 顶风少年
 * @mail dfsn19970313@foxmail.com
 * @date 2019-11-07 14:43
 * @notify 微博评论接口
 * @version 1.0
 */

@Service
public class MicroblogCommentServiceImpl implements MicroblogCommentService {

    @Autowired
    private MicroblogService microblogService;

    @Autowired
    private MicroblogCommentMapper microblogCommentMapper;

    @Autowired
    private MicroblogCommentCustomMapper microblogCommentCustomMapper;

    /*
     * @author 顶风少年
     * @date 2019/11/7 14:43
     * @mail dfsn199703132038@foxmail.com
     * @throws
     * @param
     * @param microblogComment
     * @result void
     * @notify 微博添加评论
     * version 1.0
     */
    @Transactional
    public void addMicroblogComment(MicroblogComment microblogComment) {
        //查询微博是否存在
        Microblog microblog = microblogService.getMicroblog(microblogComment.getMicroblogId());

        //如果有父类id,则给父评论的评论次数+1
        if (microblogComment.getParentid() != null) {
            MicroblogComment microblogComment1 = getMicroblogComment(microblogComment.getParentid());
            microblogComment1.setCommentNumber(microblogComment1.getCommentNumber() + 1);
            updateMicroblogComment(microblogComment1);
        } else {
            //给微博添加评论次数
            microblog.setCommentNumber(microblog.getCommentNumber() + 1);
            microblogService.updateMicroblog(microblog);
        }

        //添加默认值,添加评论
        microblogComment.setLikeNumber(0);
        microblogComment.setCommentNumber(0);
        microblogComment.setMicroblogUserId(microblog.getUserId());
        microblogComment.setDel("0");
        microblogCommentMapper.insertSelective(microblogComment);
    }

    /*
     * @author 顶风少年
     * @date 2019/11/7 15:06
     * @mail dfsn199703132038@foxmail.com
     * @throws
     * @param
     * @param id
     * @result com.datang.petstore.entity.MicroblogComment
     * @notify 根据评论id获取评论
     * version 1.0
     */
    public MicroblogComment getMicroblogComment(Integer id) {
        MicroblogComment microblogComment = microblogCommentMapper.selectByPrimaryKey(id);
        if (microblogComment == null) {
            throw new AcceptResultCodeException(ResultCode.error509);
        }else if(microblogComment.getDel().equals("1")){
            throw new AcceptResultCodeException(ResultCode.error513);
        }
        return microblogComment;
    }

    /*
     * @author 顶风少年
     * @date 2019/11/7 15:07
     * @mail dfsn199703132038@foxmail.com
     * @throws
     * @param
     * @param microblogComment
     * @result void
     * @notify 修改评论信息
     * version 1.0
     */
    @Transactional
    public void updateMicroblogComment(MicroblogComment microblogComment) {
        microblogCommentMapper.updateByPrimaryKeySelective(microblogComment);
    }

    /*
     * @author 顶风少年
     * @date 2019/11/9 20:15
     * @mail dfsn199703132038@foxmail.com
     * @throws
     * @param
     * @param getMicroblogCommentListRequest
     * @result java.util.List<com.datang.petstore.model.GetMicroblogCommentListResponse>
     * @notify 获取微博评论
     * version 1.0
     */
    public List<GetMicroblogCommentListResponse> getMicroblogCommentList(GetMicroblogCommentListRequest getMicroblogCommentListRequest) {
        String idList = getMicroblogCommentListRequest.getIdList();
        ArrayList<Integer> list = JSON.parseObject(idList, ArrayList.class);
        List<GetMicroblogCommentListResponse> microblogCommentList = microblogCommentCustomMapper.getMicroblogCommentList(getMicroblogCommentListRequest.getMicroblogiId(), getMicroblogCommentListRequest.getLastId(), list);
        return microblogCommentList;
    }

    /*
     * @author 顶风少年
     * @date 2019/11/8 17:08
     * @mail dfsn199703132038@foxmail.com
     * @throws
     * @param
     * @param getMicroblogCommentListRequest
     * @result com.datang.petstore.model.GetMicroblogCommentListResponse
     * @notify 获取微博热门评论
     * version 1.0
     */
    public List<GetMicroblogCommentListResponse> getHotMicroblogCommentList(GetMicroblogCommentListRequest getMicroblogCommentListRequest) {
        //如果微博的评论数超过50条则才会有热门评论
        Microblog microblog = microblogService.getMicroblog(getMicroblogCommentListRequest.getMicroblogiId());
        if (microblog.getCommentNumber() > 2) {
            List<GetMicroblogCommentListResponse> hotMicroblogCommentList = microblogCommentCustomMapper.getHotMicroblogCommentList(getMicroblogCommentListRequest.getMicroblogiId());
            return hotMicroblogCommentList;
        }
        return null;
    }

    /*
     * @author 顶风少年
     * @date 2019/11/10 10:44
     * @mail dfsn199703132038@foxmail.com
     * @throws
     * @param
     * @param getMicroblogCommentListRequest
     * @result java.util.List<com.datang.petstore.model.GetMicroblogTwoCommentListResponse>
     * @notify 获取微博一层评论获取二层评论
     * version 1.0
     */
    public List<GetMicroblogTwoCommentListResponse> getTwoMicroblogCommentList(GetMicroblogCommentListRequest getMicroblogCommentListRequest) {
        List<GetMicroblogTwoCommentListResponse> twoMicroblogCommentList = microblogCommentCustomMapper.getTwoMicroblogCommentList(getMicroblogCommentListRequest.getParentId(), getMicroblogCommentListRequest.getLastId());
        return twoMicroblogCommentList;
    }
}
